<html>
<head><meta charset="utf-8"><title>rvalue promotion · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html">rvalue promotion</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="156488499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156488499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156488499">(Jan 20 2019 at 16:49)</a>:</h4>
<p>Should we expect rvalue promotion to kick in for const fn at some point?</p>
<div class="codehilite"><pre><span></span><span class="k">struct</span> <span class="nc">Foo</span><span class="p">(</span><span class="kt">i32</span><span class="p">);</span><span class="w"></span>
<span class="k">impl</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">const</span><span class="w"> </span><span class="k">fn</span> <span class="nf">new</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="nc">Self</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">        </span><span class="n">Foo</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span><span class="w"></span>
<span class="w">    </span><span class="p">}</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>

<span class="k">fn</span> <span class="nf">example</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="n">Foo</span>::<span class="n">new</span><span class="p">()</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<div class="codehilite"><pre><span></span>error[E0515]: cannot return reference to temporary value
 --&gt; src/lib.rs:9:5
  |
9 |     &amp;Foo::new()
  |     ^----------
  |     ||
  |     |temporary value created here
  |     returns a reference to data owned by the current function
</pre></div>



<a name="156488511"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156488511" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156488511">(Jan 20 2019 at 16:49)</a>:</h4>
<p>Whereas these work:</p>
<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">example</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="k">static</span><span class="w"> </span><span class="n">A</span>: <span class="nc">Foo</span><span class="w"> </span><span class="o">=</span><span class="w"> </span><span class="n">Foo</span>::<span class="n">new</span><span class="p">();</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="n">A</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>


<div class="codehilite"><pre><span></span><span class="k">fn</span> <span class="nf">example</span><span class="p">()</span><span class="w"> </span>-&gt; <span class="kp">&amp;</span><span class="nb">&#39;static</span><span class="w"> </span><span class="n">Foo</span><span class="w"> </span><span class="p">{</span><span class="w"></span>
<span class="w">    </span><span class="o">&amp;</span><span class="n">Foo</span><span class="p">(</span><span class="mi">42</span><span class="p">)</span><span class="w"></span>
<span class="p">}</span><span class="w"></span>
</pre></div>



<a name="156488637"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156488637" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156488637">(Jan 20 2019 at 16:52)</a>:</h4>
<p>/cc  <span class="user-mention" data-user-id="124288">@Oli</span></p>



<a name="156488674"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156488674" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Matthew Jasper <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156488674">(Jan 20 2019 at 16:53)</a>:</h4>
<p>I think the answer is no: <a href="https://github.com/rust-lang/rust/issues/53851" target="_blank" title="https://github.com/rust-lang/rust/issues/53851">#53851</a></p>



<a name="156488893"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156488893" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156488893">(Jan 20 2019 at 16:59)</a>:</h4>
<p><span class="user-mention" data-user-id="116118">@Matthew Jasper</span> ah, thank you!</p>



<a name="156488956"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156488956" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156488956">(Jan 20 2019 at 17:00)</a>:</h4>
<p>Although I read it a bit differently: more like "we don't know what we want now, so be conservative". Maybe once we know more, we can unlock it.</p>



<a name="156520389"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156520389" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156520389">(Jan 21 2019 at 08:11)</a>:</h4>
<p>I have formulated a "no" here: <a href="https://github.com/rust-rfcs/const-eval/issues/19" target="_blank" title="https://github.com/rust-rfcs/const-eval/issues/19">https://github.com/rust-rfcs/const-eval/issues/19</a></p>



<a name="156540840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156540840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156540840">(Jan 21 2019 at 14:48)</a>:</h4>
<p>This is not what I wanted. Y u do this.</p>



<a name="156540868"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156540868" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156540868">(Jan 21 2019 at 14:49)</a>:</h4>
<p>(I mean, I'm gonna read it, and I'm sure there are good reasons)</p>



<a name="156541719"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156541719" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156541719">(Jan 21 2019 at 15:03)</a>:</h4>
<p>I'm fine with it if we invent sth like <code>&amp;const { Foo::new() }</code> blocks</p>



<a name="156541751"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156541751" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156541751">(Jan 21 2019 at 15:03)</a>:</h4>
<p>or possibly even <code>&amp;static { Foo::new() }</code>, which fits the semantics more</p>



<a name="156541853"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156541853" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156541853">(Jan 21 2019 at 15:04)</a>:</h4>
<p>I was about to say that</p>



<a name="156541908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156541908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156541908">(Jan 21 2019 at 15:05)</a>:</h4>
<p>the <code>static</code> part or the explicit blocks part?</p>



<a name="156541998"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156541998" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156541998">(Jan 21 2019 at 15:06)</a>:</h4>
<p>The static part. The const block part is clear IMO -- it's an anonymous constant, so it's just like <code>const X: _ = Foo::new(); &amp;X</code> which is distinct from promotion.</p>



<a name="156542033"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156542033" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156542033">(Jan 21 2019 at 15:07)</a>:</h4>
<p>Well, it's not promotion of the <code>Foo::new()</code> call at least</p>



<a name="156542137"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156542137" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156542137">(Jan 21 2019 at 15:08)</a>:</h4>
<p>Hm, but it works in every case where opt-in promotion would work, so there's really no need to introduce <code>static {}</code>, is there?</p>



<a name="156542525"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156542525" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156542525">(Jan 21 2019 at 15:15)</a>:</h4>
<p>The rationale for not allowing <code>&amp;Foo::new()</code> is in the link further up. But yes, trivial cases like <code>&amp;SOME_CONST</code> should not require <code>&amp;static{SOME_CONST}</code></p>



<a name="156542641"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156542641" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156542641">(Jan 21 2019 at 15:17)</a>:</h4>
<p>I know the rationale, I mean that whenever "opt-in promotion" would work, so would <code>&amp;const { &lt;expr&gt; }</code> under the usual proposed meaning of the block (anonymous constant). Hence, no need to actually introduce "opt-in promotion", it's subsumed by anonymous constants.</p>



<a name="156542654"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156542654" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Hanna Kruppe <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156542654">(Jan 21 2019 at 15:17)</a>:</h4>
<p>Despite anonymous constants being, well, constants rather than statics.</p>



<a name="156542960"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156542960" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156542960">(Jan 21 2019 at 15:22)</a>:</h4>
<p>We can't just desugar at the AST level, since we don't have constants that infer their types (and I don't think we should go down that avenue)</p>



<a name="156542989"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156542989" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156542989">(Jan 21 2019 at 15:22)</a>:</h4>
<p>But we already have anonymous constants in the compiler. Array lengths and enum discriminant inititalizers are both anonymous constants</p>



<a name="156554908"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156554908" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156554908">(Jan 21 2019 at 18:50)</a>:</h4>
<p><code>&amp;static { Foo::new() }</code> — feels too close to the generator (?) syntax of <code>static || {}</code></p>



<a name="156555160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156555160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156555160">(Jan 21 2019 at 18:56)</a>:</h4>
<p>That generator syntax is entirely temporary</p>



<a name="156555162"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156555162" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Taylor Cramer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156555162">(Jan 21 2019 at 18:56)</a>:</h4>
<p>It's not meant to stick around :)</p>



<a name="156573266"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156573266" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156573266">(Jan 22 2019 at 02:06)</a>:</h4>
<p>oh? will something need to replace it?</p>



<a name="156593015"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156593015" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Patrrick <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156593015">(Jan 22 2019 at 10:04)</a>:</h4>
<p>Test</p>



<a name="156593042"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/rvalue%20promotion/near/156593042" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Patrrick <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/rvalue.20promotion.html#156593042">(Jan 22 2019 at 10:05)</a>:</h4>
<blockquote>
<p><code>&amp;static { Foo::new() }</code> — feels too close to the generator (?) syntax of <code>static || {}</code></p>
</blockquote>
<p>This is a reply</p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>